package com.colin.app.fragment.recyclerview;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.colin.app.R;
import com.colin.app.adapter.ItemBeanRecyclerViewAdapter;
import com.colin.app.app.AppActivity;
import com.colin.app.app.AppFragment;
import com.colin.library.adapter.listener.RequestLoadMoreListener;
import com.colin.library.help.InitViewHelp;
import com.colin.library.help.LogHelp;
import com.colin.library.other.Constants;


public class RecyclerViewFragment extends AppFragment implements RecyclerViewContract.RecyclerViewView {
    private RecyclerViewContract.RecyclerViewPresenter mRecyclerViewPresenter = null;
    private RecyclerView recycler_fragment_item;
    private ItemBeanRecyclerViewAdapter adapter = null;
    private SwipeRefreshLayout swipe_fragment_refresh;

    public RecyclerViewFragment() {
    }

    private static final String FRAGMENT_ID = "fragment_id";
    private int fragment_id;

    public static RecyclerViewFragment newInstance(AppActivity mAppActivity, int fragment_id) {
        activity = mAppActivity;
        RecyclerViewFragment fragment = new RecyclerViewFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(FRAGMENT_ID, fragment_id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroyView() {
        if (null != mRecyclerViewPresenter) {
            mRecyclerViewPresenter.destroy();
            mRecyclerViewPresenter = null;
        }
        super.onDestroyView();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        fragment_id = getArguments().getInt(FRAGMENT_ID);
        super.onCreate(savedInstanceState);
        LogHelp.e(Constants.FRAGMENT_RECYCLER_VIEW_TAG[fragment_id]);
    }

    @Override
    public void onResume() {
        LogHelp.e(Constants.FRAGMENT_RECYCLER_VIEW_TAG[fragment_id] + "--onResume");
        super.onResume();
        setUserVisibleHint(true);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        LogHelp.e(Constants.FRAGMENT_RECYCLER_VIEW_TAG[fragment_id] + "--setUserVisibleHint==" + String.valueOf(isVisibleToUser));
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void initPresenter() {
        mRecyclerViewPresenter = new RecyclerViewPresenterImpl((AppActivity) activity, this, fragment_id);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.fragment_recycler_view;
    }


    @Override
    public void initView() {
        this.swipe_fragment_refresh = (SwipeRefreshLayout) this.getView().findViewById(R.id.swipe_fragment_refresh);
        this.recycler_fragment_item = (RecyclerView) this.getView().findViewById(R.id.recycler_fragment_item);
        InitViewHelp.getInstance().initSwipeRefreshLayout(swipe_fragment_refresh);
        initRecyclerView();
    }

    @Override
    public void initAsync() {
        adapter.setData(mRecyclerViewPresenter.getItemBeanList(true));
    }

    private void initRecyclerView() {
        if (adapter == null) {
            adapter = new ItemBeanRecyclerViewAdapter(activity, null, fragment_id, mListener);
        }

        adapter.addEmptyView(showEmptyView(this.recycler_fragment_item));

        adapter.openLoadingMore(true);
        RecyclerView.LayoutManager layoutManager = null;
        if (fragment_id == Constants.FRAGMENT_RECYCLER_VIEW_TYPE_GRID) {
            final int grid_columns = activity.getResources().getInteger(R.integer.grid_columns);
            layoutManager = new GridLayoutManager(activity, grid_columns);
        } else if (fragment_id == Constants.FRAGMENT_RECYCLER_VIEW_TYPE_STAGGERED) {
            final int grid_columns = activity.getResources().getInteger(R.integer.grid_columns);
            layoutManager = new StaggeredGridLayoutManager(grid_columns, OrientationHelper.VERTICAL);
        } else {
            layoutManager = new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false);
        }
        this.recycler_fragment_item.setLayoutManager(layoutManager);
        this.recycler_fragment_item.setAdapter(adapter);
    }

    @Override
    public void initListener() {
        this.swipe_fragment_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                adapter.setData(mRecyclerViewPresenter.getItemBeanList(true));
                if (null != swipe_fragment_refresh && swipe_fragment_refresh.isRefreshing()) {
                    swipe_fragment_refresh.setRefreshing(false);
                }
            }
        });

        this.adapter.setOnLoadMoreListener(new RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                swipe_fragment_refresh.post(new Runnable() {
                    @Override
                    public void run() {
                        if (mRecyclerViewPresenter.isLastPage()) {
                            adapter.notifyDataChangeAfterLoadMore(false);
                        } else {
                            adapter.notifyDataChangeAfterLoadMore(mRecyclerViewPresenter.getItemBeanList(false), true);
                        }
                    }
                });
            }
        });
    }
}
